package app.qrcode.browser.hearttoheart.adapters

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import app.qrcode.browser.hearttoheart.R

/**
 * Created by DomenCai on 2022/3/24.
 */
class LiveAdapter : RecyclerView.Adapter<LiveAdapter.LiveViewHolder>() {

    private val liveList: MutableList<Live> = mutableListOf()

    @SuppressLint("NotifyDataSetChanged")
    fun updateList(newList: ArrayList<Live>) {
        liveList.clear()
        liveList.addAll(newList)
        notifyDataSetChanged()
    }

    inner class LiveViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val teacherHead: ImageView = view.findViewById(R.id.teacherHead)
        val teacherName: TextView = view.findViewById(R.id.teacherName)!!
        val watchPeopleNum: TextView = view.findViewById(R.id.watchPeopleNum)
        val lessonName: TextView = view.findViewById(R.id.lessonName)
        val liveState: TextView = view.findViewById(R.id.liveState)
        val bgItem: ConstraintLayout = view.findViewById(R.id.item_live)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LiveViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_live, parent, false)
        return LiveViewHolder(view)
    }

    override fun onBindViewHolder(holder: LiveViewHolder, position: Int) {
        val liveTeacher = liveList[position]
        holder.teacherHead.setImageResource(liveTeacher.teacherHead)
        holder.teacherName.text = liveTeacher.teacherName
        holder.lessonName.text = liveTeacher.lessonName
        holder.liveState.text = liveTeacher.liveState
        holder.watchPeopleNum.text = liveTeacher.watchNum
        holder.bgItem.setBackgroundResource(liveTeacher.background)
    }

    override fun getItemCount(): Int = liveList.size
}